## This code replicates Figures A29, A30, A31, A32, A33

## setwd("/Users/sparshasaha/Dropbox/Seeking More Why Women Fail/Pol Behavior Rep Files Ambitious Women 02282020")
## library(cregg)

################################ FIRST ELECTION SUBSET FIRST #####################################
################################ ################################ ################################ 

## FYI Robustness Check FIRST ELECTION SUBSET (Figure A29) can be produced using code in 'Interactions AMCEs ALL SURVEYS' -- please see there for more info

mydata2 = read.csv("ssi.csv")
dim(mydata2)

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Children", "Personalistic", "Interests.Political.Affiliation", "resp_gender", "election")
mydata3 <- mydata2[,vars]

nrow(mydata3)

## subset to first election

mydata4 <- subset(mydata3, election==1)
nrow(mydata4)
data <- mydata4
nrow(data)

## Create Party4 Variable

data$Party2 <- factor(data$Interests.Political.Affiliation)
data$Party2 <- ifelse(data$Interests.Political.Affiliation == "Democrat" | data$Interests.Political.Affiliation == "Republican (GOP)", data$Party2, 0)
data$Party2[data$Party2 == 0] <- NA
data$Party3 <- factor(data$Party2)
data$Party4 <- ifelse(data$Party3=="5", "Republican", data$Party3)
data$Party4 <- ifelse(data$Party3=="2", "Democrat", data$Party4)
data$Party4 <- factor(data$Party4)

## 5 = republican, 2 = democrat

mydata <- data
nrow(mydata)

## clean this up a bit

mydata$resp_gender2 <- factor(mydata$resp_gender)

## interactions

mydata$GenderxProgressive <- with(mydata, interaction(Gender, Progressive), drop = TRUE)
mydata$GenderxAgenda <- with(mydata, interaction(Gender, Agenda), drop = TRUE)
mydata$GenderxChildren <- with(mydata, interaction(Gender, Children), drop = TRUE)
mydata$GenderxPersonalistic <- with(mydata, interaction(Gender, Personalistic), drop = TRUE)

## Code below produces Figure A31

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~resp_gender2)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm_diff", 
    by = ~resp_gender2)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)


## Code below produces Figure A30

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~Party4)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm_diff", 
    by = ~Party4)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

################################ Atypical Profiles Robustness ####################################
################################ ################################ ################################ 

library(dplyr)

mydata2 = read.csv("ssi.csv")
dim(mydata2)

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Children", "Personalistic", "Interests.Political.Affiliation", "resp_gender")
mydata3 <- mydata2[,vars]
nrow(mydata3)

##library(dplyr)
notrumps <- mydata3 %>%
  group_by(id) %>%
  filter(!any(Personalistic == "Tough Negotiator" & Gender == "Male"))

nrow(notrumps)
nrow(table(notrumps$id))

## N = 4762 OR 795 respondents

## more narrow version below

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Children", "Personalistic", "Interests.Political.Affiliation", "resp_gender")
mydata4 <- mydata2[,vars]
nrow(mydata4)

notrumps2 <- mydata4 %>%
  group_by(id) %>%
  filter(!any(Personalistic == "Tough Negotiator" & Gender == "Male" & Children == "3 children" & Progressive == "Yes" & Agenda == "Complete Overhaul"))

nrow(notrumps2)
## N = 7366

## removing Clintons from these now

notorc2 <- notrumps2 %>%
  group_by(id) %>%
  filter(!any(Personalistic == "Determined to Succeed" & Gender == "Female" & Children == "1 child" & Progressive == "Yes" & Agenda == "Moderate Changes"))

nrow(table(notorc2$id))
## N = 7210 OR 1203 respondents

##### This code below produces Figure A32 #####

data <- notorc2
nrow(data)

data$Party2 <- factor(data$Interests.Political.Affiliation)
data$Party2 <- ifelse(data$Interests.Political.Affiliation == "Democrat" | data$Interests.Political.Affiliation == "Republican (GOP)", data$Party2, 0)
data$Party2[data$Party2 == 0] <- NA
data$Party3 <- factor(data$Party2)
data$Party4 <- ifelse(data$Party3=="5", "Republican", data$Party3)
data$Party4 <- ifelse(data$Party3=="2", "Democrat", data$Party4)
data$Party4 <- factor(data$Party4)

## 5 = republican, 2 = democrat

mydata <- data
nrow(mydata)

## clean this up a bit

mydata$resp_gender2 <- factor(mydata$resp_gender)

## interactions

mydata$GenderxProgressive <- with(mydata, interaction(Gender, Progressive), drop = TRUE)
mydata$GenderxAgenda <- with(mydata, interaction(Gender, Agenda), drop = TRUE)
mydata$GenderxChildren <- with(mydata, interaction(Gender, Children), drop = TRUE)
mydata$GenderxPersonalistic <- with(mydata, interaction(Gender, Personalistic), drop = TRUE)

## model and plot

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~Party4)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm_diff", 
    by = ~Party4)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)


##### This code below produces Figure A33 #####

nrow(notrumps)
data <- notrumps
nrow(data)

data$Party2 <- factor(data$Interests.Political.Affiliation)
data$Party2 <- ifelse(data$Interests.Political.Affiliation == "Democrat" | data$Interests.Political.Affiliation == "Republican (GOP)", data$Party2, 0)
data$Party2[data$Party2 == 0] <- NA
data$Party3 <- factor(data$Party2)
data$Party4 <- ifelse(data$Party3=="5", "Republican", data$Party3)
data$Party4 <- ifelse(data$Party3=="2", "Democrat", data$Party4)
data$Party4 <- factor(data$Party4)

## 5 = republican, 2 = democrat

mydata <- data
nrow(mydata)

## clean this up a bit

mydata$resp_gender2 <- factor(mydata$resp_gender)

## interactions

mydata$GenderxProgressive <- with(mydata, interaction(Gender, Progressive), drop = TRUE)
mydata$GenderxAgenda <- with(mydata, interaction(Gender, Agenda), drop = TRUE)
mydata$GenderxChildren <- with(mydata, interaction(Gender, Children), drop = TRUE)
mydata$GenderxPersonalistic <- with(mydata, interaction(Gender, Personalistic), drop = TRUE)


## model

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~Party4)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm_diff", 
    by = ~Party4)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)





